package util.kwdDigg;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SubRobot {

	public boolean getSubNo(String kwd) {
		boolean fliter = false;
		//System.out.println(kwd);
		//System.out.println(incldeKwdList.get(subId - 1));
		// 判断是不是包含了必须有的关键字
		Pattern p = Pattern.compile(incldeKwdList.get(subId - 1), Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(kwd);
		// 判断是不是不包含了不得包含的关键字
		Pattern p2 = Pattern.compile(noIncldeKwdList.get(subId - 1), Pattern.CASE_INSENSITIVE);
		Matcher m2 = p2.matcher(kwd);
		//System.out.println(m.find() + "ccc");
		//System.out.println(m2.find() + "ddd");
		if (m.find() && !m2.find()) {
			fliter = true;
		}

		return fliter;
	}

	public void AddParameter() {
		Connection conn = null;
		Statement stmt = null;
		java.sql.ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dBName + "?user=root&password=777888999a!&useUnicode=true&characterEncoding=utf8");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from sublist");
			while (rs.next()) {
				incldeKwdList.add(rs.getString("yrex"));
				noIncldeKwdList.add(rs.getString("norex"));
				 //System.out.println(incldeKwdList.get(0));
			}
		} catch (final SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	ArrayList<String> incldeKwdList = new ArrayList<String>();
	ArrayList<String> noIncldeKwdList = new ArrayList<String>();
	String dBName = null;

	public void setdBName(String dBName) {
		this.dBName = dBName;
	}

	public void setHost(String host) {
		this.host = host;
	}

	String host = null;

	int subId = 0;

	public void setSubId(int subId) {
		this.subId = subId;
	}
}
